/*本模块功能:我的回答(老师)
        编写者:朱光曜
        编写时间:2017.11.03*/
$(function() {
	var string1 = '';
	var box1 = new parent.Confirm;
	var box = new parent.Alert;

	function formatDate(now) {//处理时间戳
		var year = now.getFullYear();
		var month = now.getMonth() + 1;
		var date = now.getDate();
		var hour = now.getHours();
		var minute = now.getMinutes();
		if(month<10){month="0"+month;}
		if(date<10){date="0"+date;}
		if(hour<10){hour="0"+hour;}
		if(minute<10){minute="0"+minute;}
		return year + "-" + month + "-" + date + "   " + hour + ":" + minute;
	}
	var loading1 = new Loading();//监控全局ajax添加loading
	loading1.init();
    $(document).ajaxSend(function(){
		loading1.show();
	
		});
	$(document).ajaxStop(function(){
		loading1.hide();
	
		});
	document.addEventListener('DOMSubtreeModified',function(e){//去除空格
		$(".publicText").attr("maxlength","20");
		
		
		$.map($(".publicText"),function (opt) {//长度限制
			maxLengthLimit.init(opt);
		});
		$(".publicText").bind('input propertychange',function(){
			
		    var regy = /\s/g;
			if(regy.test($(this).val())){
				$(this).val($(this).val().replace(/\s|\xA0/g,"")) ;
			}
		    });
	});
	function alertContent(questionContent) {//弹出内容
		var opt = "";
		for(key in questionContent.courseList) {
			opt += '<option value="1" courseId="' + questionContent.courseList[key].courseId + '">' + questionContent.courseList[key].courseName + '</option>';
		}
		return '<div id="questionUp">' +
			'<div class=" inputRight"><label for="departName">问题标题:&ensp;</label><input type="text"  class="publicText departName" /></div>' +
			'<div class=" inputRight"><label for="departNames">对应课程:&ensp;</label>' +
			'<select  class="publicSelect" />' +
			opt +
			'</select>' +
			'</div>' +
			'<div class=" inputRight"><label for="departName">问题描述:&ensp;</label>' +
			'<div id="odiv" style="display:none;position:absolute;z-index:100;">' +
			'</div>' +
			'<div onmousedown="show_element(event)" style="clear:both" id="customized-buttonpane" class="editor"></div>' +
			'</div>' +

			'</div>' +
			'<div class="clear"></div>';

	}

	function tabelModel(data) {//列表内容
		var time = new Date(data.answerList[key].problenTime);
		return '<tr class="list"  problemId="' + data.answerList[key].problemId + '">' +
			'<td class="col-lg-7"><a href="javascript:void(0)">' + data.answerList[key].problemName + '</a></td>' +
			'<td class="col-lg-3"><span class="fl">' + data.answerList[key].courseName + '</span></td>' +
            
			'<td class="col-lg-4"><span class="fl">' + formatDate(time) + '</span></td>' +
			'</tr>';

	}
	var nowData = 1;
	var index = {
		init: function() {
			this.bind();
		},
		bind: function() {
			var self = this;
			this.data_content = {}; //搜索集
			this.tab_content = {}; //列表集
			this.parameter = {
				"pageNow": 1,
				"pageSize": 10
			};

			this.postData(this.parameter, nowData);

		},
		postData: function(par_data, nowData,hunt) { //获得搜索及列表数据
			var self = this;
			var parameter = par_data;

			$.ajax({
				type: "post",
				url: "getMyAnswerListByPost",
				async: true,
				data:{
					"jsonStr":	JSON.stringify(parameter)
				},
				success: function(data, status, xhr) {
					var new_data = JSON.parse(data);
					/*var new_data =JSON.parse(ss);*/
                    if(new_data.result == "success"){
                        if(hunt) {
                            self.tab_content = new_data;

                            self.tab_add();
                        } else {

                            self.data_content = new_data;
                            self.tab_content = new_data;

                            self.add();
                            self.tab_add();
                        }
                        // console.log(new_data.totalPage+"啊啊"+new_data.pageNow)
                        var sumpage = new_data.totalPage;
                        if(sumpage<1){sumpage=1;};
                        var nowpage = 1;
                        var pagenumber = 10;
                        var parame = parameter;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {

                                nowpage = p;

                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                if(!parameter) {
                                    parameter = {
                                        "pageNow": nowpage,
                                        "pageSize": 10
                                    };

                                } else {
                                    parameter.pageNow = nowpage;

                                }
                                $.ajax({
                                    type: "post",
                                    url: "getMyAnswerListByPost",
                                    async: true,
                                    data:{
                                        "jsonStr":	JSON.stringify(parameter)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        //获取院系
                                        var msgNew = JSON.parse(msg);
                                        self.tab_content = msgNew;

                                        self.tab_add();
                                    },
                                    error: function(data, status, xhr) {
                                        var json = JSON.stringify({
                                            "pageName": "error"
                                        });
                                        window.location.href = "skipPage?jsonStr=" + json;

                                    }
                                });
                            }
                        });
					}else{
                        if(new_data.failMsg == "暂无权限"){
                            parent.location.reload(true);
                        }
					}

				},
				error: function(data, status, xhr) {
					var json = JSON.stringify({
						"pageName": "error"
				    });
					window.location.href = "skipPage?jsonStr=" + json;

				}
			});
		},
		add: function() { //搜索追加
			var self = this;
			var data = self.data_content;
			for(key in data.courseList) {
				$("#departNames").append("<option courseId=" + data.courseList[key].courseId + ">" + data.courseList[key].courseName + "</option>");
			}

			$(".funBox .searchBtn").off("click").click(function() { //搜索点击

				var courseId = $("#departNames option:selected").attr("courseId");
				var keyW = $("#keyWord").val();
				if(verification.specialCode(keyW) !== true) {
					self.alert1("不能输入特殊字符(@/'\"#$%&^*)");
					return;
				}
				var nowData = 1;
				var parameter = {
					"problemName": keyW,
					"courseId": courseId,
					"pageNow": 1,
					"pageSize": 10
				};
				var j=0;
				//遍历删除空元素
				for(i in parameter){
					if(j==0){j++;continue;}
					if(parameter[i]==null||parameter[i]==""){
						delete parameter[i];
					}
					if(!isNaN(parameter[i])){
						parameter[i]=Number(parameter[i]);
					}
				}


			
				self.postData(parameter, nowData,1);
			});
			

		},
		tab_add: function() { //列表追加

			var self = this; 
			var data = self.tab_content;
			var table = string1;

			//data.selectList={};

			var i = 0;
			for(key in data.answerList) {
				i++;
			}

			if(i > 0) {

				for(key in data.answerList) {

					table += tabelModel(data);
				}

				$(".mainContent .tableBox").html(table);
				//设置列表宽度(最大为9)
				var numMax = 0;
				$(".tableBox tr:first td").each(function() {
					var str = $(this).attr("class");
					var num = str.lastIndexOf("col-lg-") + 7;
					var num1 = parseFloat(str.substring(num, num + 1));
					numMax += num1;
				});
				
				$(".tableBox tr:first td").each(function() {
					var str = $(this).attr("class");
					var num = str.lastIndexOf("col-lg-") + 7;
					var num1 = parseFloat(str.substring(num, num + 1));
					
					$(this).css("width", num1 / numMax * 100 + "%");
				});
			} else {
				var trImg = "<tr><td colspan='3'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td/tr>";
				$(".mainContent .tableBox").html(string1+ trImg);
			}
			
			$(".tableBox tr.list td").off("click").click(function(){//点击跳转
				var json=JSON.stringify({
					"pageName":"studentQuestionsAndAnswers",
					"paramData":{
						"isMark":2,
				     
				        "problemId":parseInt($(this).parents("tr").attr("problemId"))
					}
				});
				//console.log($(this).parents("tr").html());
				window.location.href="skipPage?jsonStr="+json;
			});
		},
		alert1: function(content1) { //confirm提示

			box.contenthead = "提示";
			box.content = content1;
			
				box.success =function(){
					box.hide();
				};
			$(".alert_content", window.parent.document).css({
				
				"line-height": "70px"
			});

			box.init();
			box.show();
		}/*,
		alert2: function(content2, method) { //alert提示
            var self=this;
			box.contenthead = "我要提问";
			box.content = content2;

			
				
				box.success=function(){
					var customized_buttonpane=$("#customized-buttonpane", window.parent.document).html();
					var problemContent = escape(customized_buttonpane);
					var courseId = $('#questionUp .publicSelect  option:selected', window.parent.document).attr("courseId");
					var problemName=$(".departName", window.parent.document).val();
					
					var methodConParameter ={"problemContent":problemContent,"problemName":problemName,"courseId":courseId};	
                        console.log(methodConParameter);
							$.ajax({ 
								type: "post",
								url: "putProblem",
								async: true,
								
								data: {
									"jsonStr": JSON.stringify(methodConParameter)
								},
								success: function(msg) {
									
									var msgNew = JSON.parse(msg);
									console.log(msgNew)
									if(msgNew.result == "success") {
										self.alert1("提交成功");
									} else {
										self.alert1("提交失败");
									}

								},
								error: function(data, status, xhr) {
									var json = JSON.stringify({
						"pageName": "error"
				    });
					window.location.href = "skipPage?jsonStr=" + json;

								}
							});
						
				}
			
			box.init();
			$(".alert_box", window.parent.document).css({
				"width": "500px",
				"height": "512px"
			}).children(".alert_content").css({
				"padding-bottom": 0,
				"padding-top": "20px",
				"line-height":"34px"
			});
			$(".alert_btn", window.parent.document).css({

				"padding": "0 0 24px 0"
			});
			$(".alert_contenthead", window.parent.document).css({
				"font-size": "18px"
			});
			$(".alert_content", window.parent.document).css({
				"height": "auto",
				"min-height": "34px"
			});
			$(".alert_btn .alert_confirm", window.parent.document).html("提交");
			
			box.show();
			
			
			var backups="";
			$("#customized-buttonpane", window.parent.document).keydown(function(event) {//禁用回车跳页
				backups=box.success;
				var keycode = event.which; 
				
                if(keycode == 13) {
                	box.success =function(){};
					box.hide =function(){};
				}
				
			});
			$("#customized-buttonpane", window.parent.document).keyup(function() {//禁用回车跳页
				    box.success=backups;
					box.hide =function(){
						box.panel.style.display = "none";
						box.abs_bg.style.display = "none";
					};
				
				
			});
		}*/

	};
	index.init();
    
});